/**
 * Created by John on 2016/7/13.
 */
var MongoClient = require('mongodb').MongoClient;
var mongo = new MongoClient();
var myDB = null;

mongo.connect('mongodb://localhost/', function (err, db) {
    myDB = db.db('BeginningMongoDB');
    myDB.collection('word_stats', function (err, collection) {
        countWords(collection);
        close();
    });
});

function countWords(collection) {
    var cursor = collection.find();
    cursor.count(function (err, count) {
        console.log('Total words in the collection:');
        console.log(count);
        console.log();
    });

    var query = {first: 'a'};
    cursor = collection.find(query);
    cursor.count(function (err, count) {
        console.log('Total words starting with A:');
        console.log(count);
    });
}

function close() {
    setTimeout(function () {
        myDB.close();
    }, 3000);
}